#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

//#define N 1010
//int minCostClimbingStairs(vector<int>& cost) {
//    int dp[N] = { 0 };
//    int n = cost.size();
//    for (int i = 2; i <= n; i++)
//    {
//        dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
//    }
//    return dp[n];
//}

int main()
{
    int n;
    scanf("%d", &n);
    priority_queue<int, vector<int>, greater<int>> pq;

    for (int i = 0; i < n; i++)
    {
        int x;
        scanf("%d", &x);
        pq.push(x);
    }

    int res = 0;
    while (pq.size() > 1)
    {
        int a = pq.top(); pq.pop();
        int b = pq.top(); pq.pop();
        res += a + b;

        pq.push(a + b);
    }

    printf("%d\n", res);
    return 0;
}